cc.Class({
  extends: cc.Component,

  properties: {
    smallBellPrefab: {default: null, type: cc.Prefab},
    player: {
      default: null,
      type: cc.Node
    },
    heightSpacingA: 0,
    heightSpacingB: 0,
    widthSpacing: 0,
    smallImageWidth: 0,
    speed: 0,
  },

  // LIFE-CYCLE CALLBACKS:

  onLoad() {
    this.smallBellPool = new cc.NodePool();
    let initCount = 15;
    window.Game.player = this.player;
    for (let i = 0; i < initCount; ++i) {
      let smallBell = cc.instantiate(this.smallBellPrefab); // 创建节点
      this.smallBellPool.put(smallBell); // 通过 putInPool 接口放入对象池
    }
    this.index = 0;
    this.smallHeight = 0;
    this.time = 0;

    this.smallWidth = 0;
    for (let i = 0; i < 15; ++i) {
      this.addSmallBell();
    }
  },

  start() {

  },


  addSmallBell: function () {
    this.index = this.index + 1;

    let smallHeight = this.GetRandomNum(this.heightSpacingA, this.heightSpacingB)
    this.smallHeight = this.smallHeight + smallHeight;
    let smallWidth = this.smallWidth + this.GetRandomNum(-this.widthSpacing, this.widthSpacing);
    if (smallWidth <= (-cc.winSize.width * 0.5 + this.smallImageWidth * 0.5)) {
      smallWidth = this.GetRandomNum(-cc.winSize.width * 0.5 + this.smallImageWidth * 0.5, cc.winSize.width * 0.5 - this.smallImageWidth * 0.5);//(-cc.winSize.width*0.5 + this.smallImageWidth*0.5) ;
    }
    if (smallWidth >= (cc.winSize.width * 0.5 - this.smallImageWidth * 0.5)) {
      smallWidth = this.GetRandomNum(-cc.winSize.width * 0.5 + this.smallImageWidth * 0.5, cc.winSize.width * 0.5 - this.smallImageWidth * 0.5);//(cc.winSize.width*0.5- this.smallImageWidth*0.5);
    }
    this.smallWidth = smallWidth;
    let smallBell = null;
    if (this.smallBellPool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
      smallBell = this.smallBellPool.get();
    } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
      smallBell = cc.instantiate(this.smallBellPrefab);
    }
    smallBell.parent = this.node;
    smallBell.y = this.smallHeight - this.time * this.speed;
    smallBell.x = smallWidth;
    smallBell.tag = this.index;
    smallBell.name = "loop" + this.index;

    smallBell.getComponent('SmallBell').init(this, smallBell);

    // smallBell.getChildByName("ding").active = true
  },

  update(dt) {
    if (this.player.getComponent("Player").isbejump === false) {
      this.time = this.time + dt;
    }
    // this.node.y -= 2;

  },

  // 获取最小值到最大值之前的整数随机数
  GetRandomNum: function (Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
  }
});
